package com.wyp.didishige.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.Log;

import com.wyp.didishige.config.Config;
import com.wyp.didishige.db.dao.DaoMaster;
import com.wyp.didishige.db.dao.DaoSession;
import com.wyp.didishige.db.dao.NewCommentDao;
import com.wyp.didishige.model.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.bmob.v3.BmobUser;


/**
 * Created by Administrator on 2016/4/27.
 */
@SuppressWarnings("ALL")
public class NewCommentManager {

    private DaoMaster.DevOpenHelper openHelper;
    Context mContecxt;
    String uid=null;
    private static HashMap<String, NewCommentManager> daoMap = new HashMap<>();

    /**获取DB实例
     * @param context
     * @return
     */
    public static NewCommentManager getInstance(Context context) {
        User user = BmobUser.getCurrentUser(context, User.class);
        String loginId=user.getObjectId();
        if(TextUtils.isEmpty(loginId)){
            throw new RuntimeException("you must login.");
        }
        NewCommentManager dao = daoMap.get(loginId);
        if (dao == null) {
            dao = new NewCommentManager(context,loginId);
            daoMap.put(loginId, dao);
        }
        return dao;
    }

    private NewCommentManager(Context context, String uId){
        clear();
        this.mContecxt =context.getApplicationContext();
        this.uid=uId;
        String DBName = uId+".demodb";
        this.openHelper = new DaoMaster.DevOpenHelper(mContecxt, DBName, null);
    }

    /**
     * 清空资源
     */
    public void clear() {
        if(openHelper !=null) {
            openHelper.close();
            openHelper = null;
            mContecxt=null;
            uid =null;
        }
    }

    private DaoSession openReadableDb() {
        checkInit();
        SQLiteDatabase db = openHelper.getReadableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();
        return daoSession;
    }

    private DaoSession openWritableDb(){
        checkInit();
        SQLiteDatabase db = openHelper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();
        return daoSession;
    }

    private void checkInit(){
        if(openHelper ==null){
            throw new RuntimeException("请初始化db");
        }
    }

    //-------------------------------------------------------------

    /**获取本地所有的评论
     * @return
     */
    public List<NewComment> getAllNewComment(){
        NewCommentDao dao =openReadableDb().getNewCommentDao();
        return dao.queryBuilder().orderDesc(NewCommentDao.Properties.Id).list();
    }

    /**创建或更新新朋友信息
     * @param info
     * @return long:返回插入或修改的id
     */
    public long insertOrUpdateNewComment(NewComment info){
        NewCommentDao dao = openWritableDb().getNewCommentDao();
        NewComment local = getNewComment(info.getCommentId(),info.getStatus());
        if(local==null){
            return dao.insertOrReplace(info);
        }else{
            return -1;
        }
    }

    /**
     * 根据指定的commentId获取本地的评论
     * @param commentId
     * @param time
     * @return
     */
    private NewComment getNewComment(String commentId,Integer status){
        NewCommentDao dao =  openReadableDb().getNewCommentDao();
        return dao.queryBuilder().where(NewCommentDao.Properties.CommentId.eq(commentId))
                .where(NewCommentDao.Properties.Status.eq(status)).build().unique();
    }
    /**
     * 根据指定的commentId获取本地的评论
     * @param commentId
     * @return
     */
    private NewComment getNewComment(String commentId){
        NewCommentDao dao =  openReadableDb().getNewCommentDao();
        return dao.queryBuilder().where(NewCommentDao.Properties.CommentId.eq(commentId)).build().unique();
    }
    /**
     * 是否有新的好友邀请
     * @return
     */
    public boolean hasNewCommentNotify(){
        List<NewComment> infos = getNoSeeNewComment();
        if(infos!=null && infos.size()>0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 获取所有未读评论
     * @return
     */
    private List<NewComment> getNoSeeNewComment(){
        NewCommentDao dao =  openReadableDb().getNewCommentDao();
        return dao.queryBuilder().where(NewCommentDao.Properties.Status.eq(Config.STATUS_VERIFY_NONE))
                .build().list();
    }

    /**
     * 批量更新未读未验证的状态为已读
     */
    public void updateBatchStatus(){
        List<NewComment> infos = getNoSeeNewComment();
        if(infos!=null && infos.size()>0){
            Log.i("bmob", "updateBatchStatus: :"+infos.size());
            int size =infos.size();
            List<NewComment> all =new ArrayList<>();
            for (int i = 0; i < size; i++) {
                NewComment msg =infos.get(i);
                msg.setStatus(Config.STATUS_VERIFY_READED);
                all.add(msg);
            }
            insertBatchMessages(infos);
        }
    }

    /**批量插入消息
     * @param msgs
     */
    public  void insertBatchMessages(List<NewComment> msgs){
        NewCommentDao dao =openWritableDb().getNewCommentDao();
        dao.insertOrReplaceInTx(msgs);
    }

    /**
     * 修改指定评论的状态
     * @param uid
     * @param status
     * @return
     */
    public long updateNewComment(String commentId,Integer status){
        NewComment friend = getNewComment(commentId);
        friend.setStatus(status);
        return insertOrUpdateNewComment(friend);
    }

    /**
     * 删除指定的评论
     * @param friend
     */
    public void deleteNewComment(NewComment friend){
        NewCommentDao dao =openWritableDb().getNewCommentDao();
        dao.delete(friend);
    }

}
